﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MicroTest
{
    
    public partial class PenItem : UserControl
    {
        public delegate void ClickPenHandler(object sender, ClickPenEventArgs args);
        public event ClickPenHandler ClickPen;
        private Color labForceColor;
        public PenItem()
        {
            InitializeComponent();
            borderPen = new Pen(Color.FromArgb(0xc7, 0xd5, 0xe0),1);
            normalFont = labPen.Font;
            labForceColor=labPen.ForeColor;
        }
        private bool isSetName = false;
        private Pen borderPen;
        private Font normalFont;
        #region SelectColor
        private Color m_SelectedColor;

        [Category("Appearance")]
        [Description("Background Color when selected")]
        public Color SelectedColor
        {
            get { return m_SelectedColor; }
            set { m_SelectedColor = value; }
        }
        #endregion
        protected override void OnPaint(PaintEventArgs e)
        {
            e.Graphics.DrawRectangle(borderPen, 0, 0, this.Width - 1, this.Height - 1);
            base.OnPaint(e);
        }
        private string penSrial = string.Empty;
        public string PenSerial
        {
            set { penSrial = value;
                labPen.Text = penSrial;
                isSetName = false;
            }
            get { return penSrial; }
        }
        private bool selected = false;
        public bool Selected
        {
            set
            {
                selected = value;
                
                if(selected)
                {
                    labPen.Font = new Font(labPen.Font.FontFamily.Name, 10, FontStyle.Bold);
                    labPen.ForeColor = Color.Black;
                    pnlBase.BackColor = SelectedColor;
                }
                else
                {
                    labPen.Font = normalFont; //'//new Font(labPen.Font.FontFamily.Name, 13, FontStyle.Regular);
                    labPen.ForeColor = labForceColor;
                    pnlBase.BackColor = Color.FromArgb(245, 246, 250);
                }
            }
        }

        private Image normalImage = null;
        public Image NormalImage
        {
            set { normalImage = value;
                picPenStatus.Image = normalImage;
            }
        }
        private Image disableImage = null;
        public Image DisableImage
        {
            set
            {
                disableImage = value;
            }
        }
        private Image disconectedImage = null;
        public Image DisconectedImage
        {
            set
            {
                disconectedImage = value;
            }
        }
        private PenStatus penStatus = PenStatus.DisConnected;
        public PenStatus PenItemStatus
        {
            get
            {
                return penStatus;
            }
        }
        
        public void ChangePenStatus(PenStatus ps)
        {
            penStatus = ps;
            if (ps==PenStatus.Normal && normalImage != null)
            {
                picPenStatus.Image = normalImage;
                labPen.ForeColor= Color.FromArgb(0x77, 0x77, 0x77);
            }
                
            else if (ps==PenStatus.Disabled && disableImage != null)
            {
                picPenStatus.Image = disableImage;
                labPen.ForeColor = Color.Gray;
            }
            else if (ps == PenStatus.DisConnected && disconectedImage != null)
            {
                picPenStatus.Image = disconectedImage;
                labPen.ForeColor = Color.FromArgb(0xe7,0x65,0x3e);
            }

        }
        public void SetName(string name)
        {
            if (isSetName)
                return;
            ChangePenserialToName(name);
        }
        private void ChangePenserialToName(string name)
        {
            if(this.InvokeRequired)
            {
                Action<string> ac = new Action<string>(ChangePenserialToName);
                this.Invoke(ac, new object[] { name });
            }
            else
            {
                ELogger.Trace(string.Format("{0}：点击学生名单，对应学生为：{1}", labPen.Text, name));
                labPen.Text = name;
                isSetName = true;
            }
        }
        public string GetPenName()
        {
            if (isSetName)
                return labPen.Text;
            else
                return "";
        }
        private void labPen_Click(object sender, EventArgs e)
        {
            OnClick(e);
        }

        private void PenItem_Click(object sender, EventArgs e)
        {
            if (ClickPen != null)
            {
                ClickPenEventArgs args = new ClickPenEventArgs(penSrial);
                ClickPen(this, args);
            }

        }

        private void PenItem_MouseEnter(object sender, EventArgs e)
        {
            this.Cursor = Cursors.Hand;
        }

        private void PenItem_MouseLeave(object sender, EventArgs e)
        {
            this.Cursor = Cursors.Arrow;
        }
    }
    public enum PenStatus
    {
        Normal,
        DisConnected,
        Disabled
    }
    public class ClickPenEventArgs : EventArgs
    {
        public string ItemName { get; private set; }
        public ClickPenEventArgs(string itemName)
        {
            ItemName = itemName;
        }
    }
}
